package main

import "fmt"

type Person2 struct {
	firstName, endName string
}

/**
多个defer函数执行,当函数执行到最后时,这些defer语句会按照逆序执行
*/
func main() {

	defer funcA()
	funcB()
	defer funcC()
	fmt.Println("main 结束")
	//在函数中使用defer
	ints := []int{10, 23, 34, 4, 78, 99}
	largest := getLargest(ints)
	fmt.Printf("largest == %v \n ", largest)

	//在方法中使用defer
	p1 := Person2{"刘", "万祥"}
	defer p1.fullName()
	fmt.Printf("欢迎 , ")

	fmt.Println()
	//堆栈的延迟,打印的语句满足 先进后出
	printStr("倒过来念是猪")
}
func funcA() {
	fmt.Println("funcA 打印")
}

func funcB() {
	fmt.Println("funcB 打印")
}

func funcC() {
	fmt.Println("funcC 打印")
}

func finished() {
	fmt.Println("结束")
}
func getLargest(list []int) int {
	defer finished()
	max := 0

	for _, elem := range list {
		if elem > max {
			max = elem
		}
	}
	fmt.Printf("找到最大数为 %v \n ", max)
	return max
}

func (p *Person2) fullName() {
	fmt.Printf("full name %s %s \n", p.firstName, p.endName)
}

func printStr(str string) {
	for _, elem := range []rune(str) {
		defer fmt.Printf("%c ", elem)
	}
}
